import moment from 'moment-timezone';
import {INPUT_TYPE_ENUM} from './dateRanges';

export const FIELD_TIME_FORMAT = 'YYYYMMDDHHmm';

// 接口需求时间格式
// 分钟 YYYY-MM-DD HH:mm
const MINUTE_TIME_FORMAT = 'YYYY-MM-DD HH:mm';
// 小时：YYYYMMDDHH
const HOUR_TIME_FORMAT = 'YYYYMMDDHH';
// 日：YYYYMMDD
const DAY_TIME_FORMAT = 'YYYYMMDD';
// 周：YYYYMMDD
const WEEK_TIME_FORMAT = 'YYYYMMDD';
// 月：YYYYMM
const MONTH_TIME_FORMAT = 'YYYYMM';

export function getDateFormat(isinput) {
    switch (isinput) {
        case INPUT_TYPE_ENUM.MONTH:
            return MONTH_TIME_FORMAT;
        case INPUT_TYPE_ENUM.WEEK:
            return WEEK_TIME_FORMAT;
        case INPUT_TYPE_ENUM.DAY:
            return DAY_TIME_FORMAT;
        case INPUT_TYPE_ENUM.HOUR:
            return HOUR_TIME_FORMAT;
        case INPUT_TYPE_ENUM.MINUTE:
            return MINUTE_TIME_FORMAT;
        default:
            return DAY_TIME_FORMAT;
    }
}

export function buildTipText(list) {
    return list.map(item => (
        <div key={item.measureId}>
            <strong>
                {`${item.reportName}: `}
            </strong>
            {item.measureDescription}
        </div>
    ));
}

// 把表单的数据转换成为permission obj list
export function buildPermission(params, isinput = INPUT_TYPE_ENUM.DAY) {
    // 处理查询参数
    for (const key in params) {
        if (params[key] instanceof Array) {
            if (moment.isMoment(params[key][0])) {
                const rangeValue = params[key];
                const format = getDateFormat(isinput);
                params[key] = [rangeValue[0].format(format), rangeValue[1].format(format)];
            }
        }
        else if (typeof (params[key]) === 'string') {
            if (params[key] === '') {
                params[key] = [];
            }
            else {
                params[key] = [params[key]];
            }
        }
        else if (moment.isMoment(params[key])) {
            const pointTime = params[key];
            const format = getDateFormat(isinput);
            params[key] = [pointTime.format(format)];
        }
        else {
            params[key] = [];
        }
    }

    const keys = Object.keys(params);
    const list = keys.map(item => {
        const permission = {};
        permission.dimensionId = item;
        permission.filterItems = params[item];

        return permission;
    });

    return list;
}
